const express = require("express");
const servicePackRouter = express.Router();
const DB = require("./../db/config");

module.exports = servicePackRouter;

/**
 * 添加服务包种类
 * @param {String} spName 服务包种类名称 required
 * @param {String} spDes 服务包种类描述 required
 *
 */
servicePackRouter.route("/serivcepack/addsptype").post((req, res) => {
  let { spDes, spName } = req.body;
  let sql = `insert into service_type (stname,stdes) values(?,?);`;
  let param = [spName, spDes];
  let cb = (err, data) => {
    if (!err) {
      res.json({ code: 0, msg: "添加服务包种类成功", data: [] });
    } else {
      res.json({ code: 4002, msg: "添加服务包种类失败", data: [] });
    }
  };
  DB.connect(sql, param, cb);
});

/**
 * 添加服务包类型(级别)
 * @param {String} splName 服务包种类名称 required
 * @param {String} splDes 服务包种类描述 required
 *
 */
servicePackRouter.route("/serivcepack/addsplevel").post((req, res) => {
  let { splDes, splName } = req.body;
  let sql = `insert into service_level (slname,sldes) values(?,?);`;
  let param = [splName, splDes];
  let cb = (err, data) => {
    if (!err) {
      res.json({ code: 0, msg: "添加服务包类型(级别)成功", data: [] });
    } else {
      res.json({ code: 4002, msg: "添加服务包类型(级别)失败", data: [] });
    }
  };
  DB.connect(sql, param, cb);
});

/**
 * 获取服务包类型（级别）列表数据
 */
servicePackRouter.route("/serivcepack/getlevellist").get((req, res) => {
  let sql = `select * from service_level`;
  let cb = (err, data) => {
    if (!err) {
      res.json({
        code: 0,
        msg: "查询务包类型（级别）成功",
        data,
      });
    } else {
      res.json({
        code: 4002,
        msg: "查询务包类型（级别）失败",
        data: [],
      });
    }
  };
  DB.connect(sql, [], cb);
});
/**
 * 获取服务包种类列表数据
 */
servicePackRouter.route("/serivcepack/gettypelist").get((req, res) => {
  let sql = `select * from service_type`;
  let cb = (err, data) => {
    if (!err) {
      res.json({
        code: 0,
        msg: "查询务包类种类成功",
        data,
      });
    } else {
      res.json({
        code: 4002,
        msg: "查询务包种类失败",
        data: [],
      });
    }
  };
  DB.connect(sql, [], cb);
});

/**
 * 添加服务包详细数据
 * @param {Number} stid  服务包种类ID required
 * @param {Number} slid  服务包级别ID required
 * @param {Number} price 服务包价格 required
 *
 */
servicePackRouter.route("/serivcepack/detail/add").post((req, res) => {
  let { stid, slid, price } = req.body;
  let sql = `insert into service_detail_talbe (stid, slid, price) values(?,?,?);`;
  let param = [stid, slid, price];
  let cb = (err, data) => {
    if (!err) {
      res.json({ code: 0, msg: "添加服务包详细数据成功", data: [] });
    } else {
      res.json({ code: 4002, msg: "添加服务包详细数据失败", data: [] });
    }
  };
  DB.connect(sql, param, cb);
});

/**
 * 1.查询服务包详细列表
 * 2.按服务包种类做条件搜索
 * @param {Number} stid 服务包种类ID 可选
 * @param {Number} limit 一页显示的条数  required
 * @param {Number} page 当前页  required
 *
 */
servicePackRouter.route("/serivcepack/detail/list").get((req, res) => {
  let { stid, limit, page } = req.query;
  stid = stid ? Number(stid) : "";
  limit = Number(limit);
  page = Number(page);

  let sql = `SELECT sdt.sdid,sdt.stid,sdt.slid,sdt.price,sl.slname,st.stname FROM service_detail_talbe AS sdt ,service_level AS sl ,service_type AS st WHERE sl.slid=sdt.slid AND st.stid=sdt.stid `;
  let param = [];
  if (stid) {
    sql += " and sdt.stid = ?";
    param.push(stid);
  }
  sql += " order by sdt.sdid limit ?,?;";
  let startindex = (page - 1) * limit;
  param.push(startindex, limit);
  let sql2 = `SELECT   COUNT(*) AS count FROM   service_detail_talbe AS sdt,   service_level AS sl,   service_type AS st  WHERE sl.slid = sdt.slid    AND st.stid = sdt.stid `;
  if (stid) {
    sql2 += " and sdt.stid = ?;";
    param.push(stid);
  }
  let cb = (err, data) => {
    console.log(err);
    console.log(data[1]);
    if (!err) {
      res.json({
        code: 0,
        msg: "查询服务包详细成功",
        data: data[0],
        count: data[1][0].count,
      });
    } else {
      res.json({ code: 0, msg: "查询服务包详细失败", data: [], count: 0 });
    }
  };
  DB.connect(sql + sql2, param, cb);
});

/**
 * 修改服务包详细数据
 * @param {Number} sdid 唯一ID,
 * @param {Number} stid 服务包种类ID
 * @param {Number} slid 服务包级别ID
 * @param {Number} price 价格
 */
servicePackRouter.route("/serivcepack/detail/update").post((req, res) => {
  let { sdid, stid, slid, price } = req.body;
  let sql = `update service_detail_talbe set stid =?, slid=?,price=? where sdid =?;`;
  let param = [stid, slid, price, sdid];
  let cb = (err, data) => {
    if (!err) {
      res.json({ code: 0, msg: "更新成功", data: [] });
    } else {
      res.json({ code: 4002, msg: "更新失败", data: [] });
    }
  };
  DB.connect(sql, param, cb);
});
